package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/12 22:44
 **/
public class Day10_4_Num309_MaxProfit {
    class Solution {
        public int maxProfit(int[] prices) {
            int n = prices.length;
            if(n <= 1) return 0;
            int[] sell = new int[n];
            int[] buy = new int[n];
            int[] cool = new int[n];
            buy[0] = -prices[0];
            for(int i = 1; i < n; ++i){
                sell[i] = Math.max(buy[i-1] + prices[i], sell[i-1]);
                buy[i] = Math.max(cool[i-1] - prices[i], buy[i-1]);
                cool[i] = Math.max(sell[i-1], Math.max(buy[i-1],cool[i-1]));
            }
            return sell[n-1];
        }
    }
}